package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.UserLike;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long likeUserId) {

        if (!ifLike(userId, likeUserId)){
            UserLike userLike = new UserLike();
            userLike.setId(new ObjectId());
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(new Date().getTime());
            mongoTemplate.save(userLike);
        }
    }

    @Override
    public boolean ifLike(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        return mongoTemplate.exists(query,UserLike.class);

    }
}
